
/*
 * CommonController.java
 *
 * Created on 2006
 */

package com.qbooks.app;

import java.io.*;


import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.zll.framework.conf.Configuration;
import com.zll.framework.servlet.Command;
import com.zll.framework.util.StringUtil;




/**
 *
 * @author eastar
 * @version
 */
public class AppController extends HttpServlet {

    /**
	 *
	 */
	private static final long serialVersionUID = 1L;

	/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
    	String nextURL = "";


    	String path = request.getServletPath();
    	
    	


    	int idx = path.lastIndexOf("/");

    	String cmd = "";

    	for (int i = 0; i < path.length(); i++) {
    		//System.out.println(path.charAt(i));
    		if (path.charAt(i)== '.') {
    			break;
    		} else if (path.charAt(i) == '/') {
    			cmd += ".";
    		} else if (i == idx+1) {
    			cmd += String.valueOf(path.charAt(i)).toUpperCase();
    		} else {
    			cmd += path.charAt(i);
    		}
    	}

    	if (cmd.indexOf("android") > -1) {
    		request.setAttribute("os", "android");
    		cmd = cmd.replaceAll("android.", "");
    	} else if (cmd.indexOf("ios") > -1) {
    		request.setAttribute("os", "ios");
    		cmd = cmd.replaceAll("ios.", "");
    	}

    	System.out.println("\n\n\n serviceName:" + cmd);
    	

    	try
    	{
    		Command command = (Command)Class.forName(Configuration.getPackageName() + cmd + "Command").newInstance();
		    nextURL = command.execute(request, response);
	   	}
	   	catch (Exception e) {
	   		e.printStackTrace();
	   		request.setAttribute("exception", e);
	   		nextURL = "/common/error_page.jsp";
	   	}

   		RequestDispatcher rd = getServletContext().getRequestDispatcher(nextURL);
		rd.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
